package com.yao.util;

import com.yao.annotation.Component;
import com.yao.annotation.Controller;
import com.yao.annotation.Repository;
import com.yao.annotation.Service;

import java.lang.annotation.Annotation;

public class ReflectionUtils {
    public static final boolean isComponent(Class cls){

        if(cls.isAnnotation()||cls.isInterface())  return false;

        boolean present =
                cls.isAnnotationPresent(Component.class)
                ||
                cls.isAnnotationPresent(Repository.class)
                ||
                cls.isAnnotationPresent(Service.class)
                ||
                cls.isAnnotationPresent(Controller.class);
        return present;
    }

    public void isController(){

    }

    public static final String componentName(Class cls){

        if(cls.isAnnotation()||cls.isInterface()) return null;

        String componentName="";

        Component componentAnno = (Component) cls.getAnnotation(Component.class);

        if(componentAnno!=null){
            componentName = componentAnno.value();
        }
        Repository repoAnno = (Repository) cls.getAnnotation(Repository.class);
        if(repoAnno!=null){
            componentName = repoAnno.value();
        }

        Service serviceAnno = (Service) cls.getAnnotation(Service.class);

        if(serviceAnno!=null){
             componentName = serviceAnno.value();
        }
        Controller controllerAnno = (Controller) cls.getAnnotation(Controller.class);

        if(controllerAnno!=null){
            componentName = controllerAnno.value();
        }

        if(StringUtils.hasText(componentName)){
            return componentName;
        }
        componentName = cls.getSimpleName();
        componentName=componentName.substring(0,1).toLowerCase()+componentName.substring(1);
        return componentName;
    }
}
